package com.wuwei.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.wuwei.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuwei.gulimall.product.entity.AttrEntity;
import com.wuwei.gulimall.product.service.AttrService;
import com.wuwei.common.utils.PageUtils;
import com.wuwei.common.utils.R;



/**
 * 商品属性
 *
 * @author wuwei
 * @email 842063349@qq.com
 * @date 2022-05-25 14:45:07
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    ///product/attr/base/list/{catelogId}

    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrPage(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String attrType
                          ){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
        //删除属性的时候如果属性和分组有关联的要解除这一关联
		attrService.removeAndUnRelated(Arrays.asList(attrIds));
        return R.ok();
    }

}
